package com.zhike.b2b.util;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.saas.biz.commen.CommMethodMgr;
import com.zhike.b2b.model.UserCore;

public class PassportUtil  implements Filter
{
  Logger log = LoggerFactory.getLogger(PassportUtil.class);
	
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain fc)
    throws ServletException, IOException
  {
    request.setCharacterEncoding("UTF-8");
    response.setCharacterEncoding("UTF-8");
    
    HttpServletRequest hsr = (HttpServletRequest)request;
    HttpServletResponse res = (HttpServletResponse)response;
    HttpSession session = hsr.getSession(true);
    UserCore uc = (UserCore)session.getAttribute("OLUSER");
    String uri = hsr.getRequestURI();
    if(uri.indexOf("admin")>=0 && (uri.indexOf(".do")>=0||uri.indexOf(".jsp")>=0) && uri.indexOf("/admin/index.html")==-1)
    {
      if(uc == null)
      {
    	  res.sendRedirect("/admin/index.html");
    	  return;
      }else if(uc != null&& (!"0".equals(uc.getCustType()))){ 
    	  res.sendRedirect("/member/mainMenu/default.jsp"); 
    	  return;
       }
    }
    /*if (session.getAttribute("SESSION_USER_ID") == null)
      str1 = "";
    else
      str1 = session.getAttribute("SESSION_USER_ID").toString();
    String str2;
    if (session.getAttribute("SESSION_USER_ID") == null)
      str2 = "";
    else
      str2 = session.getAttribute("SESSION_CUST_ID").toString();
    String str3 = hsr.getRequestURI();
    String str4 = "";
    if ((str3 != null) && (str3.length() > 0) && (str3.indexOf(".") > 0))
      str4 = str3.substring(str3.lastIndexOf(".") + 1);
    if ((str3 == null) || (str4.equals("html")) || (str4.equals("html")) || (str4.equals("jsp")))
      if (str3.indexOf("downfile.jsp") < 0)
        saveLogs(hsr, str1, str2);
      else
        log.debug(str3 + "=========================");*/
    fc.doFilter(request, response);
  }

  public void saveLogs(HttpServletRequest request, String paramString1, String paramString2)
  {
	ServletContext sc = request.getSession().getServletContext();  
	ApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);  
	DataSource dataSource = (DataSource)ac.getBean("dataSource");
	JdbcTemplate jt = new JdbcTemplate(dataSource);
	
    String str1 = request.getRemoteHost();
    String str2 = request.getHeader("host");
    String str3 = request.getHeader("Referer");
    String str4 = request.getHeader("User-Agent");
    String str5 = request.getRequestURI();
    String str6 = request.getQueryString();
    String str7 = request.getRequestedSessionId();
    if (paramString1.equals(""))
      paramString1 = "0";
    if (paramString2.equals(""))
      paramString2 = "0";
    String str8 = new SimpleDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())).format(new Date());
    String str9 = " insert into logs (log_id,cust_id,user_id,user_ip,lldz,llzj,yyxx,dlsj,qqdz,qqzfc,session_id) values ('" + new CommMethodMgr().GenNumTradeId() + "','" + paramString2 + "','" + paramString1 + "','" + str1 + "'," + "'" + str3 + "','" + str2 + "','" + str4 + "','" + str8 + "','" + str5 + "','" + str6 + "','" + str7 + "') ";
   
    try
    {
    	jt.execute(str9);
    }
    catch (Exception se)
    {
      log.debug("插入流量控制语句失败！");
      se.printStackTrace();
    }
  }

  public String getCookieValue(Cookie[] paramArrayOfCookie, String paramString)
  {
    String str = "";
    if (paramArrayOfCookie == null)
      str = "";
    else
      for (int i = 0; i < paramArrayOfCookie.length; i++)
      {
        if (!paramString.equalsIgnoreCase(paramArrayOfCookie[i].getName()))
          continue;
        str = paramArrayOfCookie[i].getValue();
        return str;
      }
    return str;
  }

  public void init(FilterConfig paramFilterConfig)
    throws ServletException
  {
  }

  public void destroy()
  {
  }
}
